package com.youlai.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.youlai.system.common.exception.BusinessException;
import com.youlai.system.common.result.PageResult;
import com.youlai.system.common.result.ResultCode;
import com.youlai.system.mapper.CarouselMapper;
import com.youlai.system.model.dto.CarouselEditDTO;
import com.youlai.system.model.dto.CarouselInsertDTO;
import com.youlai.system.model.dto.CarouselSelectDTO;
import com.youlai.system.model.entity.Carousel;
import com.youlai.system.model.vo.CarouselVO;
import com.youlai.system.service.CarouselService;
import jodd.util.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Service
@Slf4j
public class CarouselServiceImpl extends ServiceImpl<CarouselMapper, Carousel>
        implements CarouselService {
    @Autowired
    private CarouselMapper carouselMapper;

    /**
     * 编辑轮播图
     * @param carouselEditDTO
     * @return
     */
    @Override
    public boolean editCarousel(CarouselEditDTO carouselEditDTO) {
        Carousel carousel = carouselMapper.getById(carouselEditDTO.getId());
        // 判断图片是否存在
        if (carousel ==null){
            throw new BusinessException(ResultCode.PARAM_VALID_ERROR);
        }
        BeanUtils.copyProperties(carouselEditDTO,carousel);
        return updateById(carousel);
    }

    @Override
    public boolean deleteCarousel(Integer[] ids) {
        for (Integer id : ids) {
            Carousel carousel = carouselMapper.getById(id);
            if (carousel == null){
                throw new BusinessException(ResultCode.PARAM_VALID_ERROR);
            }
        }
        return removeByIds(Arrays.asList(ids));
    }

    @Override
    public List<Carousel> listCarousels() {
        List<Carousel> carouselList =carouselMapper.getCarouselList();
        if (carouselList == null){
            //log.debug("没有轮播图");
            throw new BusinessException(ResultCode.PARAM_VALID_ERROR);
        }
        return carouselList;
    }

    /**
     * 添加轮播图
     * @param carouselInsertDTO
     * @return
     */
    @Override
    public boolean addCarousel(CarouselInsertDTO carouselInsertDTO) {
        // 判断图片是否存在
        Carousel existsCarousel = carouselMapper.getByUrl(carouselInsertDTO.getImageUrl());
        if (existsCarousel !=null ){
            throw new BusinessException(ResultCode.PARAM_VALID_ERROR);
        }
        Carousel carousel = new Carousel();
        BeanUtils.copyProperties(carouselInsertDTO,carousel);
        // 新增轮播图
        return save(carousel);
    }

    /**
     * 分页查询
     * @param carouselSelectDTO
     * @return
     */
    @Override
    public PageResult<CarouselVO> getPage(CarouselSelectDTO carouselSelectDTO) {
        int pageNum = carouselSelectDTO.getPageNum();
        int pageSize = carouselSelectDTO.getPageSize();
        String title = carouselSelectDTO.getTitle();
        Integer status = carouselSelectDTO.getStatus();
        String startTime = carouselSelectDTO.getStartTime();
        String endTime = carouselSelectDTO.getEndTime();
        Page<Carousel> page = this.page(
                new Page<>(pageNum,pageSize),
                new LambdaQueryWrapper<Carousel>()
                        .like(StringUtil.isNotBlank(title), Carousel::getTitle, title)
                        .eq(status !=null,Carousel::getStatus,status)
                        .ge(StringUtil.isNotBlank(startTime), Carousel::getCreatedTime, startTime)
                        .le(StringUtil.isNotBlank(endTime), Carousel::getCreatedTime, endTime)
                // 按时间倒序
                .orderByDesc(Carousel::getModifiedTime)
        );
        List<CarouselVO> carouselVOList = new ArrayList<>();
        // 转换实体类，将实体类转换为VO类
        page.getRecords().forEach(carousel ->{
//            System.out.println(carousel);
            CarouselVO carouselVO =new CarouselVO();
            BeanUtils.copyProperties(carousel,carouselVO);
//            System.out.println(carouselVO);
            carouselVOList.add(carouselVO);
        });
        return PageResult.success(carouselVOList,page.getTotal());
    }

    /**
     * 根据id查询轮播图
     * @param id
     * @return
     */
    @Override
    public CarouselVO getCarouselById(Integer id) {
        Carousel carousel = carouselMapper.getById(id);
        // 判断图片是否存在
        if (carousel == null){
            throw new BusinessException(ResultCode.PARAM_VALID_ERROR);
        }
        CarouselVO carouselVO = new CarouselVO();
        BeanUtils.copyProperties(carousel,carouselVO);
        return carouselVO;
    }
}

